package com.zdsoft.blp.zongbuju.shouye.bianmin.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.bianmin.adapter.BMzbyhlvAdapter;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.activity.FenLeiActivity;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.adapter.GridViewAdapter;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.adapter.ListViewAdapter;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.customview.MyGridView;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.customview.MyListView;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.customview.XScrollView;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.entity.FenLei;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.entity.Goods;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class BMzbyhActivity extends BaseActivity implements XScrollView.IXScrollViewListener,NetCallback{
    private XScrollView mScrollView;
    private MyGridView gv;
    private MyListView mlv;
    private ImageView iv_zb_find;
    private TextView tv_title;
    private List<FenLei> gvList ;
    private List<Goods> lvList;
    private Integer pageNo = 0;
    private BMzbyhlvAdapter adapter;
    private PopupWindow popupWindow;
    private ImageView iv_sousuo;
    private EditText et_content;
    @Override
    protected void init() {
        setContentView(R.layout.bianmin_zbyh);
        tv_title = find(R.id.tv_title);
        tv_title.setText("周边优惠");

        lvList=new ArrayList<>();
        getGnet();
        getLnet();

        mScrollView = find(R.id.sv_zb);
        mScrollView.setPullRefreshEnable(true);
        mScrollView.setPullLoadEnable(true);
        mScrollView.setAutoLoadEnable(true);
        mScrollView.setIXScrollViewListener(this);
        //实例化布局，并加载到scrollView
        View content= LayoutInflater.from(this).inflate(R.layout.zhoubianyouhui,null);
        gv= (MyGridView) content.findViewById(R.id.gv);
        mlv= (MyListView) content.findViewById(R.id.mlv);

        mScrollView.setView(content);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (gvList != null) {
                    Intent intent = new Intent(getApplicationContext(), FenLeiActivity.class);
                    intent.putExtra("name", gvList.get(position).getName().toString());
                    intent.putExtra("code", gvList.get(position).getCode().toString());
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }
            }
        });

        mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), BMspxqActivity.class);
                intent.putExtra("id",lvList.get(position).getId());
                startActivity(intent);
            }
        });
        //标题查询点击事件
        iv_zb_find = find(R.id.iv_zb_find);
        iv_zb_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPopupWindow();
                // 这里是位置显示方式,在屏幕的上侧
                popupWindow.showAtLocation(v, Gravity.TOP, 0, 0);
            }
        });
    }

    private void getPopupWindow() {
        if (null != popupWindow) {
            popupWindow.dismiss();
            return;
        } else {
            initPopuptWindow();
        }
    }

    private void initPopuptWindow() {
        // TODO Auto-generated method stub
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        View popupWindow_view = getLayoutInflater().inflate(R.layout.sousuolayout, null, false);

       iv_sousuo = (ImageView) popupWindow_view.findViewById(R.id.iv_sousuo);
       et_content = (EditText) popupWindow_view.findViewById(R.id.et_content);
        iv_sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = et_content.getText().toString().trim();
                if (content!=null&&!"".equals(content)){
                    Intent intent = new Intent(getApplicationContext(), FenLeiActivity.class);
                    intent.putExtra("name", content);
                    intent.putExtra("type", 7);
                    startActivity(intent);
                }else {
                    Toast.makeText(getApplicationContext(),"内容不能为空",Toast.LENGTH_SHORT).show();
                }
            }
        });
        // 创建PopupWindow实例,100,LayoutParams.MATCH_PARENT分别是宽度和高度
        popupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, 100, true);
        // 设置动画效果
        // popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 点击其他地方消失
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });
    }

    private void getLnet() {
        Net net = new Net();
        net.url = API.GOODS_URL;
        net.nameSpace = API.GOODS_NAMESPACE;
        net.methodName = API.SPGOODSLIST;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 10);
        map.put("goodsCategoryCode", "");
        map.put("type", "D");
        map.put("curTime", new Date().getTime() + "");
        map.put("communityCode", communityCode);
        map.put("condition", "");
        net.parms = map;
        net.code = 2;
        getServer(net, this);
    }

    private void getGnet() {
        Net net=new Net();
        net.url = API.CATEGORY_URL;
        net.nameSpace = API.CATEGORY_NAMESPACE;
        net.methodName = API.CATEGORYROOTLIST;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String,Object> map=new HashMap<>();
        map.put("communityCode",communityCode);
        net.code=1;
        net.parms=map;
        getServer(net,this);

    }
    @Override
    public void onRefresh() {
        gvList=new ArrayList<>();
        lvList=new ArrayList<>();
        pageNo=0;
        getGnet();
        getLnet();
    }

    @Override
    public void onLoadMore() {
        getLnet();
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag&&code==1){
            gvList=new ArrayList<>();
            try {
                JSONArray array = new JSONArray(res);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    gvList.add(new FenLei(object.getString("columnImgPath"), object.getString("name"), object.getString("id"), object.getString("code"), object.getString("childNum")));
                }
                gv.setAdapter(new GridViewAdapter(gvList,getApplicationContext()));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        if (flag&&code==2){
            try {
                JSONObject object = new JSONObject(res);
                JSONArray array = object.getJSONArray("rows");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    lvList.add(new Goods(jsonObject.getString("total"), jsonObject.getString("summary"), jsonObject.getString("oldPrice"),
                            jsonObject.getString("canBuy"), jsonObject.getString("limitNum"), jsonObject.getString("discountPrice"), jsonObject.getString("goodsCode"),
                            jsonObject.getString("goodsNum"), jsonObject.getString("picPath"), jsonObject.getString("id"), jsonObject.getString("peopleBuy"),
                            jsonObject.getString("idsort"), jsonObject.getString("tgoodscategory"), jsonObject.getString("discountStartDate"), jsonObject.getString("goodsName"),
                            jsonObject.getString("discountEndDate"), jsonObject.getString("tgoodscategoryid"), jsonObject.getString("merchantname")));

                }
                if (pageNo == 0) {
                    adapter = new BMzbyhlvAdapter(lvList, getApplicationContext());
                    mlv.setAdapter(adapter);
                    pageNo++;
                    mScrollView.stopRefresh();
                } else {
                    mScrollView.stopLoadMore();
                    adapter.setLists(lvList);
                    adapter.notifyDataSetChanged();
                }
                pageNo++;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }
}
